{extend name="lib/common_list" /}
{block name="title"}新增角色{/block}
{block name="css"}
    <link rel="stylesheet" href="/static/sys/css/common.css">
    <link href="/static/plugins/jsTree/style.min.css" rel="stylesheet">
{/block}
{block name="javascript"}
    <script src="/static/plugins/jsTree/jstree.min.js"></script>
    <script type="text/javascript">
         $(document).ready(function () {
            $("#cancel").bind("click", function () {
                parent.layer.close(parent.layer_add_index);
            });
            $("#ok").bind("click", Save);
            $("#jstree1").jstree({
                "core": {
                    "check_callback": true
                },
                "checkbox": {
                    "keep_selected_style": false                    
                },
                "plugins": ["types", "dnd", "checkbox", "wholerow"],
                "types": {
                    "default": {
                        "icon": "fa fa-folder"
                    },
                    "css": {
                        "icon": "fa fa-file-code-o"
                    }
                }
            });
         })

         function Save() {
            var roleName = $.trim($("input[name=role_name]").val());
            if (roleName.length == 0) {
                layer.tips('角色名称不能为空！', 'input[name=role_name]', { tips: [2, '#78BA32'] });
                layer.alert('角色名称不能为空！');
                return false;
            }
            var cheked_arr = Array();
            cheked_arr = $("#jstree1").jstree("get_checked");
            $("#jstree1").find(".jstree-checked, .jstree-undetermined").each(function () {
                cheked_arr.push($(this).parents("li").attr("id"));
            });           
            var load_index = layer.load(0, { shade: false });
            $.ajax({
                type: "post",
                url: "{url('role/add')}?t=" + new Date(),
                data: { role_name: roleName, remark: $.trim($("textarea[name=remark]").val()), permCodes: cheked_arr.join(",") },
                dataType: "json",
                success: function (result) {
                    layer.close(load_index);
                    if (result.code == 0) {
                        layer.alert('保存成功！', function () {
                            if (typeof (parent.initTable) == "function")
                                parent.initTable();
                            parent.layer.close(parent.layer_add_index)
                        });
                    } else {
                        layer.alert(result.msg);
                    }
                },
                error:function(xhr,textStatus){
                    layer.close(load_index);
                    layer.alert('服务器繁忙！');
                }
            });
        }
    </script>
{/block}
{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form method="post" id="fmNew" class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><i class="icon-asterisk">*</i>角色名称：</label>
                                <div class="col-sm-8">
                                    <input type="text" name="role_name" class="form-control" placeholder="角色名称">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">描述：</label>
                                <div class="col-sm-8">
                                    <textarea class="form-control diff-textarea" name="remark"></textarea>                                
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">                            
                                <label class="col-sm-2 control-label">角色权限：</label>
                                <div class="col-sm-8">
                                    <div id="jstree1" style="height:380px; overflow:auto">
                                        {$prmhtml}
                                    </div>                               
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-12" style="text-align:center;">
                                    <button class="btn btn-primary" id="ok" type="button">提交</button>
                                    <button class="btn btn-white" id="cancel" type="button">取消</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>        
{/block}